package zero.tech.games.mj.util;

import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.mj.data.MjGameBase;
import zero.tech.games.mj.data.MjResGameBase;
import zero.tech.games.mj.data.MjResReconnect;
import zero.tech.games.mj.data.MjRoomStatus;
import zero.tech.games.mj.data.MjTimer;
import zero.tech.games.mj.data.consts.GameConst;
import zero.tech.games.mj.data.entity.PlayerData;
import zero.tech.games.mj.data.notify.NotifyCtrlCard;
import zero.tech.games.mj.data.notify.NotifyGameStart;
import zero.tech.games.mj.data.notify.NotifyOutCard;
import zero.tech.games.mj.data.res.ResSpCtrlCard;
import zero.tech.wsnet.controller.LogicController;

/**
 * 数据包装工具类
 * @author Administrator
 *
 */
public class PackageUtil {
	
	/**
	 * 通知游戏开始
	 * @param room
	 * @param player
	 * @return
	 */
	public static NotifyGameStart packageNotifyGameStart(Room room, PlayerData player) {
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		NotifyGameStart builder = new NotifyGameStart();
		/** 房间状态 */
		builder.setRoomStatus(room.getRoom_status());
		/** 玩家id */
		builder.setUid(player.getUid());
		/** 场次局数 */
		builder.setTimes(room.getRoom_times());
		/** 庄家id */
		builder.sethUid(gameBase.getHuid());
		/** 剩余牌数量 */
		builder.setLastNum(gameBase.getCardBank().size());
		/** 玩家的手牌 */
		builder.setCards(player.getHandCard());
		/** 当局鬼牌 */
		builder.setGuiCards(gameBase.getGuiCards());
		/** 当前操作玩家id */
		builder.setOperUid(gameBase.getOperData().getUid());
		/** 操作倒计时 */
		builder.setCountDown(GameConst.DA_PAI_TIME);
		return builder;
	}

	/**
	 * 通知玩家特殊操作牌(1碰,2明杠,3胡,4暗杠,5点杠,)
	 * @param player
	 * @param card
	 * @return
	 */
	public static ResSpCtrlCard packageResSpCtrlCard(PlayerData player, int card) {
		ResSpCtrlCard builder = new ResSpCtrlCard();
		/** 操作的牌 */
		builder.setCard(card);
		/** 操作时间 */
		builder.setTime(GameConst.DA_PAI_TIME);
		/** 类型(2,3,4)2碰,3明杠,4胡,5暗杠,6点杠，7自摸 "1_12,11|1_12,14|7_11|8"  "类型_牌,牌|类型_牌,牌" */
		builder.setInfo(GameUtils.getTypeInfo(player.getActionArr()));
		return builder;
	}
	
	
	/**
	 * 广播玩家操作(1过,2碰,3明杠,4胡,5暗杠,6点杠)  
	 * @param player
	 * @param type
	 * @param card
	 * @param room
	 * @return
	 */
	public static NotifyCtrlCard packageNotifyCtrlCard(PlayerData player, int type, int card, Room room) {
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		NotifyCtrlCard builder = new NotifyCtrlCard();
		/** 玩家ID */
		builder.setUid(player.getUid());
		/** 牌库剩余牌数 */
		builder.setLastNum(gameBase.getCardBank().size());
		/** 所有操作过的牌 //玩家碰杠牌信息  说明：类型_牌,牌,牌_玩家ID_玩家的位置|.... 类型：2碰,3明杠,4胡,5暗杠,6点杠 */
		builder.setCtrlArr(GameUtils.listStrToStringByl(player.getCtrlCard()));
		/** 操作类型（1过,2碰,3明杠,4胡,5暗杠,6点杠，7自摸） */
		builder.setType(type);
		/** 当前操作的牌 */
		builder.setCard(card);
		/** -1(未胡)   1.2.3（胡的顺序） */
		builder.setHuSort(player.getHuSort());
		/** 玩家手牌数量(不包含已操作牌) */
		builder.setHandCardSize(player.getHandCard().size());
		return builder;
	}
	
	/**
	 * 广播有玩家打牌
	 * @param player
	 * @param card
	 * @return
	 */
	public static NotifyOutCard packageNotifyOutCard(PlayerData player, int outCard) {
		NotifyOutCard builder = new NotifyOutCard();
		/** 出牌玩家id */
		builder.setUid(player.getUid());
		/** 出的牌 */
		builder.setCard(outCard);
		/** 玩家出牌后手牌数量 */
		builder.setHandCardSize(player.getHandCard().size());
		return builder;
	}
	
	/**
	 * 中途恢复数据
	 * @param room
	 * @param online
	 * @return
	 */
	public static ResReconnet packageResPdkReconnect(Room room, LogicController online) {
		ResReconnet builder = new MjResReconnect();
		PckUtil.packageResReconnect(room,online,builder);
		int room_status=room.getRoom_status();
		if(room_status == MjRoomStatus.HUAN_ZHANG || room_status == MjRoomStatus.DING_QUE || room_status == MjRoomStatus.DA_PAI) {
			builder.setRes_gamebase(packageResMjGameBase(room,online));
		}
		return builder;
	}
	
	/**
	 * 游戏中数据
	 * @param room
	 * @param online
	 * @return
	 */
	private static MjResGameBase packageResMjGameBase(Room room, LogicController online) {
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		MjTimer mjTimer = (MjTimer) room.getTimer();
		MjResGameBase builder = new MjResGameBase();
		PlayerData player = gameBase.getPlayerUid(online.getId());
		String outCardsStr = "";
		String ctrlArrsStr = "";
		String huStateStr = "";
		String cardNumsStr = "";
		for (PlayerData playerTemp : gameBase.getPlayerMap().values()) {
			outCardsStr += playerTemp.getUid() + "_"
					+ GameUtils.listToString(playerTemp.getOutCard()) + "|";
			ctrlArrsStr += playerTemp.getUid() + "##"
					+ GameUtils.listStrToStringByl(playerTemp.getCtrlCard())
					+ "$$";
			huStateStr += playerTemp.getUid() + "_" + playerTemp.getHuType() + "_"
					+ GameUtils.listToString(playerTemp.getHuCards()) + "_"
					+ playerTemp.getHuSort() + "|";
			cardNumsStr += playerTemp.getUid() + "_" + playerTemp.getHandCard().size() + "|";
		}
		/** 房间状态 4:换牌阶段, 5:打定确定阶段, 6:打牌中 */
		builder.setRoomStatus(room.getRoom_status());
		/** 自己手牌 */
		builder.setCards(player.getHandCard());
		/** 所有玩家打出去的牌  "1_12,13|玩家id_牌,牌...|.....|" */
		outCardsStr = outCardsStr.substring(0, outCardsStr.length() - 1);
		builder.setOutCards(outCardsStr);
		/** 所有操作过的牌 //玩家碰杠牌信息  说明：(  玩家id##类型_牌,牌,牌_玩家ID_玩家的位置|.... 类型：2碰,3明杠,4胡,5暗杠,6点杠)$$(类型_牌,牌,牌_玩家ID_玩家的位置|.... 类型：2碰,3明杠,4胡,5暗杠,6点杠) */
		ctrlArrsStr = ctrlArrsStr.substring(0, ctrlArrsStr.length() - 2);
		builder.setCtrlArrs(ctrlArrsStr);
		/** 手牌数量 "玩家id_数量|玩家id_数量" */
		cardNumsStr = cardNumsStr.substring(0, cardNumsStr.length() - 1);
		builder.setCardNums(cardNumsStr);
		/** 庄家id */
		builder.setHostUid(gameBase.getHuid());
		/** 当前操作玩家id */
		builder.setCtrlUid(gameBase.getOperData().getUid());
		/** 当前剩余操作时间 */
		Long lastTime = 15000L - (System.currentTimeMillis() - mjTimer.getLastTimer());
		lastTime = lastTime > 0 ? lastTime : 0;
		builder.setCtrlTime(lastTime);
		/** 当前操作的牌(打到堂子的牌) 定缺或者换牌 默认 -1 */
		builder.setCtrlCard(gameBase.getCurrCard());
		/** 牌库剩余牌数 */
		builder.setLastNum(gameBase.getCardBank().size());
		/** "玩家id_类型_牌_顺序...|"   胡牌类型： 4胡,7自摸  顺序:胡牌的顺序 */
		huStateStr = huStateStr.substring(0, huStateStr.length() - 1);
		builder.setHuState(huStateStr);
	    /** 类型(2,3,4)2碰,3明杠,4胡,5暗杠,6点杠，7自摸 "1_12,11|1_12,14|7_11|8"  "类型_牌,牌|类型_牌,牌" 如果是“” 就是自己没有任何操作 */
		builder.setInfo(GameUtils.getTypeInfo(player.getActionArr()));
	    /** 当局鬼牌 */
		builder.setGuiCards(gameBase.getGuiCards());
		return builder;
	}

}
